package com.project.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;

public class TestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    public void init() throws ServletException {
        System.out.println(123);
    }

    public TestServlet() {
        System.out.println("执行servlet初始化");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //设置请求信息的编码集，只对POST有效
        req.setCharacterEncoding("utf-8");
        //设置响应信息的MIME类型的编码集，防止中文乱码
        resp.setContentType("text/html;charset=utf-8");
        //根据表单名得到客户端的表单数据值
        String name = req.getParameter("name");
        //得到多个同名的键值对信息
        String [] likeArray = req.getParameterValues("hobby");
        //得到打印字符流，可以向客户端输出文本数据
        PrintWriter w=resp.getWriter();
        w.println("hello,你好世界 表单数据 name = "+name+"\n");
        w.println(Arrays.toString(likeArray));
        //得到写入的字节流，可以向客户端发送二进制数据
        OutputStream outputStream= resp.getOutputStream();
    }
}
